package inet.app.vod.activity.fun;

import inet.app.vod.activity.R;
import inet.app.vod.activity.common.BaseActivity;
import inet.app.vod.config.AppConstants;
import inet.app.vod.facebook.CommentView;
import inet.app.vod.object.FunModel;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.inet.app.framework.bitmap.BitmapUtil;
import com.inet.app.framework.bitmap.UniversalImageUtil;
import com.inet.app.framework.device.DeviceUtil;
import com.inet.app.framework.widget.MyToast;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class IFunDetailActivity extends BaseActivity {

	private FunModel mFun;

	private View.OnClickListener shareFbListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			shareItem("iFun", mFun.getFbLink(), mFun.getTitle(), mFun.getImg());
		}
	};

	private void initModels() {

		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			mFun = (FunModel) bundle.getSerializable("fun");
		}
		if (mFun == null) {
			MyToast.show(this, "Không tồn tại ảnh này");
			finish();
			return;
		}
	}

	private void initViews() {

		/******************** bind UI *************************************/
		findViewById(R.id.share_button).setOnClickListener(shareFbListener);
		TextView titleText = (TextView) findViewById(R.id.title_text);
		TextView viewText = (TextView) findViewById(R.id.num_view_text);
		ImageView imageView = (ImageView) findViewById(R.id.image_view);
		CommentView commentView = (CommentView) findViewById(R.id.comment_layout);
		commentView
				.loadComments(mFun.getFbLink());

		/******************** attach model ********************************/
		titleText.setText(String.valueOf(mFun.getTitle()));
		viewText.setText(String.valueOf(mFun.getViewNo()));
		final int widthDevice = DeviceUtil.getSimpleDeviceInfo(this).getWidth();
		UniversalImageUtil.getInstance(mContext).displayImage(mFun.getImg(),
				imageView, new SimpleImageLoadingListener() {
					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {

						int width = loadedImage.getWidth();
						int height = loadedImage.getHeight();
						int heightScale = (int) ((widthDevice * 1.0f / width) * height);
						// scale image to fit screen and below limit size
						// (2048x2048)
						loadedImage = BitmapUtil.limitSizeBitmap(loadedImage,
								widthDevice, heightScale);
						if (loadedImage == null) {
							view.setVisibility(View.GONE);
							return;
						}
						((ImageView) view).setImageBitmap(loadedImage);
					}

				});
		setTitle(AppConstants.NAME_IFUN);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ifun_detail);
		initUIBase();
		initModels();
		initViews();
	}

	@Override
	public void setTagMenu() {
		mTagMenu = AppConstants.MENU_IFUN;
	}

}
